{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba4a6587",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<img src=\"https://github.com/jina-ai/discoart/blob/main/.github/banner.png?raw=true\" alt=\"Create Disco Diffusion artworks in one-line\">\n",
    "<p align=center>\n",
    "<a href=\"https://pypi.org/project/discoart/\"><img src=\"https://img.shields.io/pypi/v/discoart?style=flat-square&amp;label=Release\" alt=\"PyPI\"></a>\n",
    "<a href=\"https://slack.jina.ai\"><img src=\"https://img.shields.io/badge/Slack-3.1k-blueviolet?logo=slack&amp;logoColor=white&style=flat-square\"></a>\n",
    "<a href=\"https://colab.research.google.com/github/jina-ai/dalle-flow/blob/main/client.ipynb\"><img src=\"https://img.shields.io/badge/Open-in%20Colab-brightgreen?logo=google-colab&style=flat-square\" alt=\"Open in Google Colab\"/></a>\n",
    "</p>\n",
    "\n",
    "😎 **If you are already a DD user**: you are ready to go! There is no extra learning, DiscoArt respects the same parameter semantics as DD5.2. So just unleash your creativity! [Read more about DiscoArt vs. DD5.5](https://github.com/jina-ai/discoart/blob/main/FEATURES.md)\n",
    "\n",
    "You can always do `from discoart import cheatsheet; cheatsheet()` to check all new/modified parameters, DiscoArt specific parameters are highlighted in the cheatsheet. \n",
    "\n",
    "\n",
    "\n",
    "👶 **If you are a [DALL·E Flow](https://github.com/jina-ai/dalle-flow/) or new user**: you may want to take step by step, as Disco Diffusion works in a very different way than DALL·E. It is much more advanced and powerful: e.g. Disco Diffusion can take weighted & structured text prompts; it can initialize from a image with controlled noise; and there are way more parameters one can tweak. Impatient prompt like `\"armchair avocado\"` will give you nothing but confusion and frustration. I highly recommend you to check out the following resources before trying your own prompt:\n",
    "- [Zippy's Disco Diffusion Cheatsheet v0.3](https://docs.google.com/document/d/1l8s7uS2dGqjztYSjPpzlmXLjl5PM3IGkRWI3IiCuK7g/mobilebasic)\n",
    "- [EZ Charts - Diffusion Parameter Studies](https://docs.google.com/document/d/1ORymHm0Te18qKiHnhcdgGp-WSt8ZkLZvow3raiu2DVU/edit#)\n",
    "- [Disco Diffusion 70+ Artist Studies](https://weirdwonderfulai.art/resources/disco-diffusion-70-plus-artist-studies/)\n",
    "- [A Traveler’s Guide to the Latent Space](https://sweet-hall-e72.notion.site/A-Traveler-s-Guide-to-the-Latent-Space-85efba7e5e6a40e5bd3cae980f30235f#e122e748b86e4fc0ad6a7a50e46d6e10)\n",
    "- [Disco Diffusion Illustrated Settings](https://coar.notion.site/Disco-Diffusion-Illustrated-Settings-cd4badf06e08440c99d8a93d4cd39f51)\n",
    "- [Coar’s Disco Diffusion Guide](https://coar.notion.site/coar/Coar-s-Disco-Diffusion-Guide-3d86d652c15d4ca986325e808bde06aa#8a3c6e9e4b6847afa56106eacb6f1f79)\n",
    "\n",
    "# Install"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca392ef6",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "!pip install -U discoart"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47428f37",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "---\n",
    "\n",
    "# Create artworks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78f39415",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from discoart import create\n",
    "\n",
    "da = create()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6269fd20",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "That's it. It is that simple."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d36983b6",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Specify parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbf748c5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from discoart import create\n",
    "\n",
    "da = create(text_prompts='A painting of sea cliffs in a tumultuous storm, Trending on ArtStation.',\n",
    "       init_image='https://d2vyhzeko0lke5.cloudfront.net/2f4f6dfa5a05e078469ebe57e77b72f0.png',\n",
    "       skip_steps=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6ac122d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Check parameter cheatsheet\n",
    "\n",
    "But what parameters can be used in `create()`? just type `cheatsheet()` to lookup anytime:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c6c8306",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from discoart import cheatsheet\n",
    "\n",
    "cheatsheet()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d893ac95",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "🎉 That's all you need to learn.\n",
    "\n",
    "Of course if you have some extra time, it would be helpful for you to learn the following API of DiscoArt.\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "## Visualize results\n",
    "\n",
    "\n",
    "Final results and intermediate results are created under the current working directory, e.g.\n",
    "```text\n",
    "./{name-docarray}/{i}-step-{j}.png\n",
    "./{name-docarray}/{i}-progress.png\n",
    "./{name-docarray}/{i}-progress.gif\n",
    "./{name-docarray}/{i}-done.png\n",
    "```\n",
    "\n",
    "![](.github/result-persist.png)\n",
    "\n",
    "where:\n",
    "\n",
    "- `name-docarray` is the name of the run, you can specify it otherwise it is a random name.\n",
    "- `i-*` is up to the value of `n_batches`.\n",
    "- `*-done-*` is the final image on done.\n",
    "- `*-step-*` is the intermediate image at certain step.\n",
    "- `*-progress.png` is the sprite image of all intermediate results so far.\n",
    "- `*-progress.gif` is the animated gif of all intermediate results so far.\n",
    "\n",
    "The save frequency is controlled by `save_rate`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df5020e4",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Export configs\n",
    "\n",
    "You can review its parameters from `da[0].tags` or export it as an SVG image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a190a7c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from discoart.config import save_config_svg\n",
    "\n",
    "save_config_svg(da, 'my.svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe0d4d89",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Pull results anywhere anytime\n",
    "\n",
    "At anytime on any machine, you can pull the real-time results (including paramters, intermedidate diffusion steps, final results) with a session ID:\n",
    "\n",
    "> Please replace `discoart-3205998582` to your own when you run the above 2 cells!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9db8e4e6",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from docarray import DocumentArray\n",
    "\n",
    "da = DocumentArray.pull('discoart-3205998582')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ca59262",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Reuse a Document as initial state\n",
    "\n",
    "Consider a Document as a self-contained data with config and image, one can use it as the initial state for the future run. Its `.tags` will be used as the initial parameters; `.uri` if presented will be used as the initial image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d29a95b0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from discoart import create\n",
    "from docarray import DocumentArray\n",
    "\n",
    "da = DocumentArray.pull('discoart-3205998582')\n",
    "\n",
    "create(init_document=da[0],\n",
    "       cut_ic_pow=0.5,\n",
    "       tv_scale=600, \n",
    "       cut_overview='[12]*1000', \n",
    "       cut_innercut='[12]*1000', \n",
    "       use_secondary_model=False)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
